package com.zcx.net;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.zcx.app.DollApplication;
import com.zcx.common.Helper;
import com.zcx.util.HttpUtil;
import com.zcx.util.LogUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okio.ByteString;

/**
 * Created by hp on 2017/9/22.
 */

public class OkHttpHelp {

    private static final long TIME = 5 * 1000;

    public static void initOkHttp() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(TIME, TimeUnit.MILLISECONDS);
        builder.writeTimeout(TIME, TimeUnit.MILLISECONDS);
        builder.connectTimeout(TIME, TimeUnit.MILLISECONDS);
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));
        OkGo.getInstance()
                .init(DollApplication.getInstance())
                .setOkHttpClient(builder.build());
    }



    public static void postOkUserHttp(String url, String content, BaseCallback callback) {
        if (DollApplication.isSystem) {
            return;
        }
        if (!hasNet(callback)) {
            return;
        }
        url = UrlHelper.getLastUrl("https://api.91lezhua.com/api/User/UnifiedOrder?av=1.0.1&imei=863670035162318&sv=6.0.1&mt=OPPO R9s&pid=6&ns=1&c=_360");
        String header = "";
        header = "Bearer" + " " + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJmZjlkOWQyYy02NTViLTQ5NTQtYmYyZi1mMjYwMzcxMTg0MzkiLCJpYXQiOiIyMDE4LzMvNiA4OjMyOjA0IiwibmFtZWlkIjoiNTM4NzI1IiwidHlwIjoiMSIsIm5iZiI6MTUyMDMyNTEyNCwiZXhwIjoxNTIwOTI5OTI0LCJpc3MiOiJ0ZXN0IiwiYXVkIjoidGVzdCJ9.lXrG-TNDuIRrPEFQlzpPDJTnopbO0MCFMClxzI0psg0";
        OkGo.<String>post(url)
                .retryCount(2)
                .headers("Authorization", header)
                .upString(content)
                .execute(callback);
    }

    private static OkHttpClient client = null;

    //单例模式获取OkHttpClient的实例。
    public static OkHttpClient getInstance() {
        if (client == null) {
            synchronized (HttpUtil.class) {
                if (client == null)
                    client = new OkHttpClient();
            }
        }
        return client;
    }

    //Okhttp3--Post请求
    public static void sendOkHttpPostRequest(final String address, final okhttp3.Callback callback){
        LogUtil.d("aaaa",address);
        Gson gson = new Gson();
        FormBody formBody = new FormBody.Builder()
                .add("orderId","47668a5cbd9848bf8d179365b449cece")
                .build();
        Request request = new Request.Builder()
                .url(address)
                .post(formBody)
                .build();
        getInstance().newCall(request).enqueue(callback);
    }

    //Okhttp3--Get请求
    public static void sendOkHttpGetRequest(final String address, final okhttp3.Callback callback) {
        Request request = new Request.Builder()
                .url(address)
                .build();
        getInstance().newCall(request).enqueue(callback);
    }


    public static void getOkHttp(String url, BaseCallback callback) {
        if (!hasNet(callback)) {
            return;
        }
        OkGo.<String>get(url).retryCount(2).execute(callback);
    }

    public static void downloadFile(String downloadPath, FileCallback fileCallback) {
        OkGo.<File>get(downloadPath).retryCount(2).execute(fileCallback);
    }

    public static boolean hasNet(BaseCallback callback) {
        if (NetWorkHelper.isNetworkAvailable()) {
            return true;
        } else {
            if (Helper.isNotNull(callback)) {
                callback.onError(null);
            }
            return false;
        }
    }

    public static OkHttpClient getHttpClient() {
        return OkGo.getInstance().getOkHttpClient();
    }

}
